package com.sh.activitystudy;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

public class ActRequestActivity extends AppCompatActivity implements View.OnClickListener {

    private String request = "你睡了吗";
    private ActivityResultLauncher<Intent> register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_act_request);
        TextView request_one = findViewById(R.id.tv_request_one);
        request_one.setText("发送的消息：" + request);

        Button requestBtn = findViewById(R.id.btn_request);
        TextView tv_response = findViewById(R.id.tv_response);
        requestBtn.setOnClickListener(this);

        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult o) {
                if (o !=null ){
                    Intent data = o.getData();
                    if (data != null && o.getResultCode()== ActRequestActivity.RESULT_OK){
                        Bundle extras = data.getExtras();
                        String string = extras.getString("respones");
                        tv_response.setText(string);
                    }
                }
            }
        });

    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(this, ActResponseActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("req" , request);
        intent.putExtras(bundle);
        register.launch(intent);
    }
}